package com.qeeyou.apps.android.craftsman.activity.me;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.adapter.SystemNoticeAdapter;
import com.qeeyou.apps.android.craftsman.po.PushMsgInfo;
import com.qeeyou.apps.android.craftsman.service.AppService;
import com.qeeyou.apps.android.craftsman.view.CustomLoadingDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SysMessageActivity extends Activity {

    private ImageView btnBack;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;
    private SystemNoticeAdapter mAdapter;
    private List<PushMsgInfo> noticeList = new ArrayList<PushMsgInfo>();

    private CustomLoadingDialog mLoadingDialog;

    private int mPageNo = 1;
    private int mPageCount = 10;

    private boolean mIsLoadMore = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_message);

        setView();

        setEvent();
    }

    private void setView() {
        btnBack = (ImageView) this.findViewById(R.id.btnBack4SystemMessage);
        mSwipeRefreshLayout = (SwipeRefreshLayout) this.findViewById(R.id.srlNotice4SystemMessage);
        mRecyclerView = (RecyclerView) this.findViewById(R.id.rvNotice4SystemMessage);
    }

    private void setEvent() {
        mSwipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorBlue));
//        mSwipeRefreshLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        mAdapter = new SystemNoticeAdapter(SysMessageActivity.this, noticeList);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(SysMessageActivity.this, NoticeDetailActivity.class);
                SysMessageActivity.this.startActivity(intent);
                Toast.makeText(SysMessageActivity.this, "Item点击事件!", Toast.LENGTH_SHORT).show();
            }
        });

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPageNo = 1;
                mIsLoadMore = false;
                new GetPushMessageList().execute();
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPageNo++;
                mIsLoadMore = true;
                new GetPushMessageList().execute();
            }
        }, mRecyclerView);

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        SysMessageActivity.this.finish();
                    }
                }, 500);
            }
        });

        mPageNo = 1;
        mIsLoadMore = false;
        new GetPushMessageList().execute();
    }

    //消息列表
    class GetPushMessageList extends AsyncTask<String, Void, List<PushMsgInfo>> {

        public GetPushMessageList() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (mLoadingDialog == null) {
                mLoadingDialog = new CustomLoadingDialog(SysMessageActivity.this);
            }
            mLoadingDialog.setMessage("正在获取消息列表...");
            mLoadingDialog.show();
        }

        @Override
        protected List<PushMsgInfo> doInBackground(String... params) {
            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("pageIndex", String.valueOf(mPageNo));
            paramMap.put("pageSize", String.valueOf(mPageCount));
            List<PushMsgInfo> pushMsgList = AppService.getInstance().getPushMessageList(paramMap);
            return pushMsgList;
        }

        @Override
        protected void onPostExecute(List<PushMsgInfo> masterList) {
            super.onPostExecute(masterList);
            if (mLoadingDialog != null) {
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
            }
            if (!mIsLoadMore) {
                mSwipeRefreshLayout.setRefreshing(false);
                noticeList.clear();
            }

            if (masterList == null || masterList.size() == 0) {
                mAdapter.loadMoreEnd(!mIsLoadMore);
            } else {
                noticeList.addAll(masterList);
                mAdapter.setItems(noticeList);
                mAdapter.loadMoreComplete();
            }
        }
    }
}
